<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\common\logic\export;





use app\common\logic\ExportService;
use app\common\model\shop\Goods;
use think\Db;
use think\exception\ValidateException;

class NiuExport
{
    private static $new; //申请一个私有的静态成员变量来保存该类的唯一实例
    const TITLE_ARR = [
        '序号'         => [1, 1, 9],  //0为开始  1为结束
        '牛的名称'                 => [2,2, 12],
        '编号'                       =>[3,3, 12],
        '销量'        => [4,4, 10],
        '英文名称'                              => [5,5, 12] ,
        '是否上架'                   =>[6,6, 9],
        '价格'                          =>[7,7, 10],
        '库存'                          =>[8,8, 10],
        '出售方式'                          =>[9,9, 10],
        'gtpi值'                          =>[10,10, 10],
        '净效应指数'                          =>[11,11, 10],
        '产奶育种值'                          =>[12,12, 10],
        '乳房指数'                          =>[13,13, 10],
        '肢蹄指数'                          =>[14,14, 10],
        '公牛配种难产率'                          =>[15,15, 10],
        '生产寿命'                          =>[16,16, 10],
        '女儿妊娠率'                          =>[17,17, 10],
    ];
    const SUBMIT = [
        'author' =>'育牛',
        'subject' =>'育牛',
        'title' =>'育牛导出',
    ];
    private function __construct() {

    }


    public static function instance () { //声明一个静态公共方法，供外部获取唯一实例
        if (!(self::$new instanceof self)) {
            self::$new = new self;
        }
        return self::$new;
    }




    /**
     * 生成税率 报表
     */
    public function export() {
        $name = '牛品种导出';
        $titleArr = self::TITLE_ARR;
        $submit = self::SUBMIT;

        //list($sheetName, $sort, $line, $sheet) = [ $timeStr . '-表0', 1, 1, 0]; //初始化变量
        list($sheetName, $sort, $line, $sheet) = [$name, 1, 1, 0]; //初始化变量
        $excel = ExportService::instance()->setTitles($submit);

        //初始化
        $excel->createSheet($sheetName, $sheet)->setSheetHeader($line, $titleArr);
        $list = Goods::order('sales', 'desc')->select();

        foreach ($list as $k => $v) {
            $line ++;
            $value = [
                $sort,
                $v['name'],
                $v['code'],
                $v['sales'],
                $v['enname'],
                $v['status'] == 0 ? '上架' : '仓库',
                $v['money'] ,
                $v['stock'] ,
                $v['type'] == 0 ? '面议' : '在线出售',
                $v['gtpi'],
                $v['real_money'],
                $v['milk_pro'],
                $v['breast'],
                $v['foot'],
                $v['beast'],
                $v['year'],
                $v['birth'],
            ];
            $excel->insertValues($line, $value);
            $sort ++;
        }

        $excel->output('Xlsx', $name);
        return true;
    }

    private function __clone() {} //声明私有的克隆方法，防止对象被克隆

    public function __sleep() { //重写__sleep方法，将返回置空，防止序列化反序列化获得新的对象
        return [];
    }
}